<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>js中的json对象</title>
</head>
<body>
    <script>
        // 定义一个json对象
        var jsonObj = {
            "name": '张三',
            "age": 18,
            "gender": '男',
            "hobby": ['篮球', '足球', '羽毛球'],
            "action": function() {
                console.log('json对象中的方法')
            }
        }
        console.log(jsonObj);
        console.log(jsonObj.name);
        console.log(jsonObj.hobby[1]);
        // 定义一个json字符串
        var jsonStr = '{"name":"Tom", "age":18, "addr":["北京","上海","西安"]}';
        console.log(jsonStr);
        console.log(jsonStr.name);
        // 字符串转json对象
        var v1 = JSON.parse(jsonStr);
        console.log(v1.name);
        // json对象转字符串
        var v2 = JSON.stringify(jsonObj);
        console.log(v2);
    </script>
</body>
</html>